/**  
 * All rights Reserved, Designed By Android_Robot   
 * @Title:  GlideUtil.java   
 * @Package com.swift.utils   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: HYM     
 * @date:   2017-5-18 上午11:21:14   
 * @version V1.0     
 */
package com.swift.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.util.Util;
import com.swift.R;
import com.swift.base.ApplicationBase;

import java.io.File;

/**
 * @ClassName: GlideUtil
 * @Description:TODO(//加入了计算高度的方法)
 * @author: HYM
 * @date: 2017-5-18 上午11:21:14
 * 
 */
public class GlideUtil {
	public static void loadIntoUseFitWidth(Context context,
			final String imageUrl, final ImageView imageView) {
		Glide.with(context)
				.load(imageUrl)
				.asBitmap()
				.listener(new RequestListener<String, Bitmap>() {
									@Override
									public boolean onException(Exception e, String model,
											Target<Bitmap> target,
											boolean isFirstResource) {
										return false;
									}

									@Override
									public boolean onResourceReady(Bitmap resource,
											String model, Target<Bitmap> target,
											boolean isFromMemoryCache, boolean isFirstResource) {
										if (imageView == null) {
											return false;
										}
//										if (ivPic.getScaleType() != ImageView.ScaleType.FIT_XY) {
//											ivPic.setScaleType(ImageView.ScaleType.FIT_XY);
//										}
										ViewGroup.LayoutParams params = imageView
												.getLayoutParams();
										int vw = imageView.getWidth()
												- imageView.getPaddingLeft()
												- imageView.getPaddingRight();
										Drawable drawable = new BitmapDrawable(resource);
										float scale = (float) vw
												/ (float) drawable.getIntrinsicWidth();
										int vh = Math.round(drawable.getIntrinsicHeight()
												* scale);
										params.height = vh + imageView.getPaddingTop()
												+ imageView.getPaddingBottom();
										imageView.setLayoutParams(params);
										return false;
									}
								})
//				.diskCacheStrategy(DiskCacheStrategy.SOURCE)
//				.listener(new RequestListener<String, GlideDrawable>() {
//					@Override
//					public boolean onException(Exception e, String model,
//							Target<GlideDrawable> target,
//							boolean isFirstResource) {
//						return false;
//					}
//
//					@Override
//					public boolean onResourceReady(GlideDrawable resource,
//							String model, Target<GlideDrawable> target,
//							boolean isFromMemoryCache, boolean isFirstResource) {
//						if (imageView == null) {
//							return false;
//						}
////						if (imageView.getScaleType() != ImageView.ScaleType.FIT_XY) {
////							imageView.setScaleType(ImageView.ScaleType.FIT_XY);
////						}
//						ViewGroup.LayoutParams params = imageView
//								.getLayoutParams();
//						int vw = imageView.getWidth()
//								- imageView.getPaddingLeft()
//								- imageView.getPaddingRight();
//						float scale = (float) vw
//								/ (float) resource.getIntrinsicWidth();
//						int vh = Math.round(resource.getIntrinsicHeight()
//								* scale);
//						params.height = vh + imageView.getPaddingTop()
//								+ imageView.getPaddingBottom();
//						imageView.setLayoutParams(params);
//						return false;
//					}
//				})
//				.placeholder(errorImageId)
//				.error(errorImageId)
				.into(imageView);
	}
	
	public static void loadIntoUseFitWidth(Context context,
			final File filePath, final ImageView imageView) {
		Glide.with(context)
				.load(filePath)
				.asBitmap()
				.listener(new RequestListener<File, Bitmap>() {
									@Override
									public boolean onException(Exception e, File model,
											Target<Bitmap> target,
											boolean isFirstResource) {
										return false;
									}

									@Override
									public boolean onResourceReady(Bitmap resource,
											File model, Target<Bitmap> target,
											boolean isFromMemoryCache, boolean isFirstResource) {
										if (imageView == null) {
											return false;
										}
										ViewGroup.LayoutParams params = imageView
												.getLayoutParams();
										int vw = imageView.getWidth()
												- imageView.getPaddingLeft()
												- imageView.getPaddingRight();
										Drawable drawable = new BitmapDrawable(resource);
										float scale = (float) vw
												/ (float) drawable.getIntrinsicWidth();
										int vh = Math.round(drawable.getIntrinsicHeight()
												* scale);
										params.height = vh + imageView.getPaddingTop()
												+ imageView.getPaddingBottom();
										imageView.setLayoutParams(params);
										return false;
									}
								})
				.into(imageView);
	}
	
	public static void load(Context context,String path,ImageView imageView)
	{
		if (Util.isOnMainThread()) {
			Glide.with(context).load(path).error(R.mipmap.ic_launcher).into(imageView);
		}
		else {
			Glide.with(ApplicationBase.Context).load(path).error(R.mipmap.ic_launcher).into(imageView);
		}
	}
	
	public static void loadInFitXY(Context context,String path,ImageView imageView)
	{
		if (Util.isOnMainThread()) {
			Glide.with(context).load(path).fitCenter().error(R.mipmap.ic_launcher).into(imageView);
		}
		else {
			Glide.with(ApplicationBase.Context).load(path).fitCenter().error(R.mipmap.ic_launcher).into(imageView);
		}
	}
}
